// pagesFarm/pages/goldenManage/index.js
var app = getApp(),
  fetchApi = require("../../../utils/httpPost.js"),
  formatTime = require("../../../component/common.js")
import Toast from '../../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoading:true,
    account:{},
    eggToYd:0,//鸡蛋兑换物业金数
    goldenToYd:0,
    eggCount:0,
    leftUserCount:0,
    isShowEggExchangeYd:false,
    isShowGoldenExchangeYd:false,
    exchangeEggCount:0,
    exchangeYdCount:0,
    reg_new_user_remark:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.onHttpInit();
  },
  onHttpInit() {
    var that = this;
    fetchApi.httpPost('/account/my/personal/detail', {
    }, function (res) {
      console.log("我的账户信息", res)
      if (res.data.code == 0) {
        that.data.leftUserCount = res.data.data.leftUserCount;
        that.setData({
          account: res.data.data.account,
          eggToYd: res.data.data.eggToYd,
          goldenToYd: res.data.data.goldenToYd,
          eggCount: res.data.data.eggCount,
          leftUserCount: res.data.data.leftUserCount,
          reg_new_user_remark: res.data.data.reg_new_user_remark,
        })
      }
      that.setData({
        netWork: true,
        isLoading: false,
      })
    }, function () {
      that.setData({
        netWork: false,
        hasData: false,
        isLoading: false,
      })
    })
  },
  goEggExchangeYd(e){
    if(this.data.leftUserCount > 0){
      this.setData({
        isShowEggExchangeYd:true
      })
    }else{
      Toast("您新用户可用邀请数不足,赶紧邀请新用户吧~")
    }
  },
  goGoldenExchangeYd(e){
    if(this.data.leftUserCount > 0){
      this.setData({
        isShowGoldenExchangeYd:true
      })
    }else{
      Toast("您新用户可用邀请数不足,赶紧邀请新用户吧~")
    }
  },
  
  onChangeEggCount(e){
    console.log('onChangeEggCount',e);
    this.data.exchangeEggCount = e.detail
    this.setData({
      exchangeEggCount:e.detail
    })
  },
  onChangeGoldenCount(e){
    console.log('onChangeGoldenCount',e);
    this.data.exchangeYdCount = e.detail
    this.setData({
      exchangeYdCount:e.detail
    })
  },
  
  onEggExchangeYdClose(e){
    console.log('onExchangeYdClose',e);
    if(e.detail == "confirm"){//确认
      this.onHttpEggExchangeYd();
    }
  },
  onGoldenExchangeYdClose(e){
    console.log('onGoldenExchangeYdClose',e);
    if(e.detail == "confirm"){//确认
      this.onHttpGoldenExchangeYd();
    }
  },
  // 鸡蛋兑换物业金
  onHttpEggExchangeYd(){
    var that = this;
    if(that.data.exchangeEggCount == 0){
      Toast.fail("请输入大于0的数");
      return;
    }
    fetchApi.httpPost('/farm/eggChangeYd', {
      egg_count:that.data.exchangeEggCount,
    }, function (res) {
      console.log("兑换金豆数据", res)
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
        that.onHttpInit();
      }else{
        Toast.fail(res.data.msg);
      }
    }, function () {})
  },
  // 金豆兑换物业金
  onHttpGoldenExchangeYd(){
    var that = this;
    if(that.data.exchangeYdCount == 0){
      Toast.fail("请输入大于0的数");
      return;
    }
    fetchApi.httpPost('/farm/goldenToYd', {
      yd_count:that.data.exchangeYdCount,
    }, function (res) {
      console.log("兑换金豆数据", res)
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
        that.onHttpInit();
      }else{
        Toast.fail(res.data.msg);
      }
    }, function () {})
  },
  goNotice1(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/notice/detail/index?id=' + id,
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    var that = this;
    var user_id = app.getStorageSync("user_id");
    console.log("转发成功 user_id",user_id)
    return {
      title: that.data.reg_new_user_remark,
      path: '/pages/home/home?ag_id='+user_id,
      success: function (res) {
        console.log(res, "转发成功")
      },
      fail: function (res) {
        console.log(res, "转发失败")
      }
    }
  },  
})